<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="姓名"></el-input>
                </el-form-item>
                <el-select v-model="filters.state" placeholder="请选择">
                    <el-option label="全部" value=""></el-option>
                    <el-option label="上架" value="1"></el-option>
                    <el-option label="下架" value="0"></el-option>
                </el-select>
                <el-form-item>
                    <el-button type="primary" v-on:click="getUsers">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="productAdd">上架</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="productDown">下架</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--列表-->
        <el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" label="编号">
            </el-table-column>
            <el-table-column prop="name" label="名称" sortable>
            </el-table-column>
            <el-table-column prop="saleprice" label="售价"  sortable>
            </el-table-column>
            <el-table-column prop="costprice" label="成本价"  sortable>
            </el-table-column>
            <el-table-column prop="state" label="状态" :formatter="formatSex" width="100" sortable>
            </el-table-column>
            <el-table-column prop="offsaletime" label="下架时间" sortable>
            </el-table-column>
            <el-table-column prop="onsaletime" label="上架时间" sortable>
            </el-table-column>
            <el-table-column prop="salecount" label="销量" sortable>
            </el-table-column>

            <el-table-column label="操作">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="total, sizes, prev, pager, next, jumper"
                           @current-change="handleCurrentChange"
                           @size-change="handleSizeChange"
                           :current-page="currentPage"
                           :page-size="pageSize"
                           :page-sizes="pageSizes"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增界面-->
        <el-dialog title="编辑" v-model="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm"  label-width="80px">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="售价" prop="saleprice">
                    <el-input v-model="addForm.saleprice" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="成本价" prop="costprice">
                    <el-input v-model="addForm.costprice" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item prop="logo" label="资源">
                    <el-upload
                            class="upload-demo"
                            action="http://localhost:80/common/fastDfs/"
                            :on-preview="handlePreview"
                            :on-remove="handleRemove"
                            :on-success="handleSuccess"
                            :file-list="fileList"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="简介" prop="productDetail.intro">
                    <!--<el-input v-model="addForm.productDetail.intro" :options="QuillOption" auto-complete="off"></el-input>-->
                    <quill-editor v-model="addForm.productDetail.intro" :options="QuillOption"></quill-editor>
                </el-form-item>
                <el-form-item label="预约须知" prop="productDetail.orderNotice">
                    <!--<el-input v-model="addForm.productDetail.orderNotice" auto-complete="off"></el-input>-->
                    <quill-editor v-model="addForm.productDetail.orderNotice" :options="QuillOption"></quill-editor>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    import { quillEditor } from 'vue-quill-editor'
    import QuillConfig from '../../common/js/quill-config'

    export default {
        data(){
            return{
                QuillOption:QuillConfig, //富文本编辑器
                fileList:[],         //上传图片成功后返回的
                addFormVisible:false,//模态框弹出 默认关闭
                users:[],            //双向绑定 列表数据
                listLoading: false,  //加载框
                sels:[],             //列表选中项
                currentPage:1,       //当前页
                pageSize:5,          //默认显示每页条数
                pageSizes:[5,10,10], //每页条数
                total:0,             //总条数
                filters:{            //高级查询
                    name:'',
                    state:''         //状态
                },
                addForm:{            //新增 修改模态框
                    name:'',
                    saleprice:'',
                    costprice:'',
                    resources:'',
                    productDetail:{
                        intro:'',
                        orderNotice:''
                    }
                },
                productPrep:{  //解决新增清空数据会清空intro...等字段问题
                    intro:'',
                    orderNotice:''
                },
                components:{
                    quillEditor
                }
            }
        },
        methods:{
            //上传成功
            handleSuccess(response, file, fileList){
                console.log("===========");
                console.log("上传成功",response);
                console.log(file);
                console.log(fileList);
                for(var i=0; i<fileList.length; i++){
                    var fileObj = fileList[i];  //获取图片对象
                    var url = fileObj.response.result;  //获取对象地址
                    if(i==0){
                        this.addForm.resources=url;
                    }else {
                        this.addForm.resources=this.addForm.resources+","+url;
                    }
                }
            },
            //删除图片
            handleRemove(file, fileList) {
                console.debug("删除图片",file);
                console.debug(fileList);
                var url="";
                if(file.response){
                    url = file.response.result;
                }else {
                    url = file.url.substring(this.fdfsUrl.length)
                }
                let partm = {"url":url};
                this.$http.post("/common/delete",partm).then(res=>{
                    if(res.data.success){
                        if(file.length==0){
                            //没有图片
                            this.addForm.fileList="";
                            return;
                        }
                        for(var i=0; i<fileList.length; i++){
                            var fileObj = fileList[i];  //获取图片对象
                            var url = fileObj.response.result;  //获取对象地址
                            if(i==0){
                                this.addForm.resources=url;
                            }else {
                                this.addForm.resources=this.addForm.resources+","+url;
                            }
                        }
                    }
                })
            },
            handlePreview(file) {
                console.log(file);
            },
            //状态 模板
            formatSex(row){
                return row.state==1?"上架":"下架"
            },
            //当页码发生改变时 触发
            handleCurrentChange(val){
                this.currentPage = val;
                this.loadProduct();
            },
            //当每页条数发生变化时 触发
            handleSizeChange(val){
                this.currentPage = 1;
                this.pageSize = val;
                this.loadProduct();
            },
            selsChange(sels){
                this.sels = sels;
            },
            //批量删除
            batchRemove(){
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.patch("/product/batchDelProduct",ids).then((res) => {
                        if(res.data.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.loadProduct();
                        }else {
                            this.$message.error(res.data.msg)
                        }
                    });
                }).catch(() => {

                });
            },
            //编辑按钮
            handleEdit(index,row){
                this.fileList=[];
                this.addFormVisible = true;
                console.debug("编辑按钮",row);
                //回显
                for(var k in row){
                    this.addForm[k] = row[k]
                }
                if(row.productDetail){  //产品详情回显
                    this.addForm.productDetail.orderNotice=row.productDetail.orderNotice;
                    this.addForm.productDetail.intro=row.productDetail.intro;
                }
                if(row.resources){ //上传图片回显
                    var imgArr=row.resources.split(",");
                    for(var i=0; i<imgArr.length;i++){
                        this.fileList.push({"url":this.fdfsUrl + imgArr[i]})
                    }
                }
            },
            //删除按钮
            handleDel(index,row){
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    console.debug("删除测试；",row);
                    this.$http.delete("/product/"+row.id).then((res) => {
                        if(res.data.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.loadProduct();
                        }else {
                            this.$message.error(res.data.msg)
                        }
                    });
                }).catch(() => {

                });
            },
            //高级查询 按钮
            getUsers(){
                this.loadProduct();
            },
            //添加 按钮
            handleAdd(){
                this.addFormVisible = true;
                //console.debug("新增按钮：",this.addForm);
                for(var k in this.addForm){
                    this.addForm[k] = "";
                }
                for(var a in this.productPrep){
                    this.productPrep[a] = "";
                }
                this.addForm.productDetail= this.productPrep;
            },
            //上架
            productAdd(){
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认上架该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.patch("/product/productAdd",ids).then((res) => {
                        if(res.data.success){
                            this.$message({
                                message: '上架成功',
                                type: 'success'
                            });
                            this.loadProduct();
                        }else {
                            this.$message.error(res.data.msg)
                        }
                    });
                }).catch(() => {

                });
            },
            //下架
            productDown(){
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认下架该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.patch("/product/productDown",ids).then((res) => {
                        if(res.data.success){
                            this.$message({
                                message: '下架成功',
                                type: 'success'
                            });
                            this.loadProduct();
                        }else {
                            this.$message.error(res.data.msg)
                        }
                    });
                }).catch(() => {

                });
            },
            //加载员工列表
            loadProduct(){
                let partm = {
                    "currentPage":this.currentPage,
                    "pageSize":this.pageSize,
                    "name":this.filters.name,
                    "state":this.filters.state,
                };
                this.$http.patch("/product",partm).then((res => {
                    this.users = res.data.list;
                    this.total = res.data.total;
                    console.debug("加载员工列表",res);
                }));
            },
            //添加 修改 模态框确认按钮
            addSubmit(){
                let parp = Object.assign({},this.addForm);
                //parp.productDetail.intro = this.addForm.productDetail.intro;
                console.debug("参数",parp)
                console.debug("修改确认按钮",this.addForm.id);
                console.debug("修改表单的值",this.addForm);
                if(this.addForm.id){
                    this.$http.post("/product",parp).then((res => {
                        if(res.data.success){
                            this.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                            this.addFormVisible=false;
                            this.loadProduct();
                        }else {
                            this.$message.error(res.data.msg);
                        }
                    }));
                }else {
                    this.$http.put("/product",parp).then((res => {
                        if(res.data.success){
                            this.$message({
                                message: '添加成功',
                                type: 'success',
                            });
                            this.addFormVisible = false;
                            this.loadProduct()
                        }else {
                            this.$message.error(res.data.msg);
                        }
                    }));
                }
            }
        },
        mounted(){
            this.loadProduct();
        }
    }
</script>